<?php
// +----------------------------------------------------------------------
// | [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Author < 931274989@qq.com >
// +----------------------------------------------------------------------
// | Explain 订单
// +----------------------------------------------------------------------
namespace app\ymkj\controller;

use EasyWeChat\Foundation\Application;
use EasyWeChat\Payment\Order;
use think\facade\Session;
use think\facade\Config;
use think\Controller;
use think\Db;

class Wechat1 extends Controller
{
  // 生成订单
  public function save()
  {
    $name = Session('name');
    $order_id = Session('order_id');
    $price = Session('price');
    $openid = Session('openid');
    $options = Config::get('wewhat.wxConfig');

    // 商品信息
    $product = [
      'trade_type'       => 'JSAPI', // 微信公众号支付填JSAPI
      'body'             => $name,
      'out_trade_no'     => $order_id, // 订单号
      'total_fee'        => $price, // 单位：分
      'notify_url'       => url('Orders/notify'), // 支付结果通知网址，如果不设置则会使用配置里的默认地址
      'openid'           => $openid, // trade_type=JSAPI，此参数必传，用户在商户appid下的唯一标识，
      // ...
    ];
    $order = new Order($product);
    $app = new Application($options);
    $payment = $app->payment;
    $result = $payment->prepare($order); // 这里的order是上面一步得来的。 这个prepare()帮你计算了校验码，帮你获取了prepareId.省心。
    $prepayId = null;
    if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS'){
      $prepayId = $result->prepay_id; // 这个很重要。有了这个才能调用支付。 
      $json = $payment->configForPayment($prepayId);
      $this->assign([
        'jsonData' => json_decode($json,true),
        'order_id' => $order_id,
        'price' => $price,
      ]);
      return view();
    } else {
      return ['status' => 400, 'msg' => '调起支付失败，请稍后尝试'];  
    }
  }

  // 支付回调
  public function notify()
  {
    return view();
  }

  // 支付成功
  public function chenggong(){
    $data = request()->param();
    $user = Session::get('wewhat_user');
    $start_time = time();
    $end_time = time()+365*24*60*60;
    $chongzhi = Db::name('order')->where('id','=',$data['order_id'])->value('name');
    $order = Db::name('order')->where('id','=',$data['order_id'])->update(['type' => 1]);
    if($chongzhi == '会员充值'){
      $this->fenxiao($data['price']);
      $result = Db::name('user')
      ->where('openid','=',$user['openid'])
      ->update(['vip'=>1,'bolishui'=>12,'start_time' =>$start_time,'end_time'=>$end_time]);
    }
    if($order){
      return view();
    } else {
      dump($data['order_id']); die;
    }
  }

  // 分销
  public function fenxiao($jine)
  {
    $user = Session::get('wewhat_user');
    $fenxiao = Db::name('fenxiao')->where('fenxiao','=','fenxiao')->find();
    $wxuser = Db::name('user')->where('openid','=',$user['openid'])->find();
    if($fenxiao['status'] == 1 && $fenxiao['type'] == 1){ // 百分比分销
      $f1 = Db::name('user')->where('openid','=',$wxuser['f1'])->setInc('balance',$jine*$fenxiao['f1']/100);
      $f2 = Db::name('user')->where('openid','=',$wxuser['f2'])->setInc('balance',$jine*$fenxiao['f2']/100);
      $f3 = Db::name('user')->where('openid','=',$wxuser['f3'])->setInc('balance',$jine*$fenxiao['f3']/100);
    }
    if($fenxiao['status'] == 1 && $fenxiao['type'] == 2){ // 固定金额
      $f1 = Db::name('user')->where('openid','=',$wxuser['f1'])->setInc('balance',$fenxiao['f1']*100);
      $f2 = Db::name('user')->where('openid','=',$wxuser['f2'])->setInc('balance',$fenxiao['f2']*100);
      $f3 = Db::name('user')->where('openid','=',$wxuser['f3'])->setInc('balance',$fenxiao['f3']*100);
    }
  }
}
?>